const app = getApp()
const util = require("../../utils/utils.js")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    userimg: "",
    nickname: "",
    isCollected: null,
    isInCart: null,
    loginUserInfo:{},
  },

  // scroll(e) {
  //   console.log(e)
  // },

  // onClickIcon() {
  //   wx.showToast('点击图标');
  // },

  // onClickButton() {
  //   wx.showToast('点击按钮');
  // },

  onCartButton() {
    var user = app.globalData.loginAccount
    var dealing = this.data.item.id
    var that = this
    wx.request({
      url: 'http://localhost:8888/cart/insertCart',
      method: 'POST',
      data: {
        user: user,
        dealing: dealing
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        wx.showToast({
          title: '加入购物车成功',
          icon: "success"
        })
        that.setData({
          isInCart: 1
        })
      }
    })
  },
  onCartOffButton() {
    var user = app.globalData.loginAccount
    var dealing = this.data.item.id
    var that = this
    wx.request({
      url: 'http://localhost:8888/cart/deleteCart',
      method: 'POST',
      data: {
        user: user,
        dealing: dealing
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        wx.showToast({
          title: '移出购物车成功',
          icon: "success"
        })
        that.setData({
          isInCart: 0
        })
      }
    })
  },
  onBuyButton() {
    var user = app.globalData.loginAccount
    var seller = this.data.item.username
    var dealing = this.data.item.id
    var itemname = this.data.item.itemname
    wx.request({
      url: 'http://localhost:8888/buying/insertBuying',
      method: 'POST',
      data: {
        username: user,
        dealing: dealing
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        wx.request({
          url: 'http://localhost:8888/chatting/sendMsg',
          method: 'POST',
          data: {
            user1: "sys",
            user2: seller,
            content: "您的商品（"+itemname+"）被购买，请及时查看"
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
          }
        })
        wx.showToast({
          title: '提交请求成功',
          icon: "success"
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "商品详情"
    });
    var that = this
    that.setData({
      loginUserInfo: app.globalData.userInfo
    })
    var username = app.globalData.loginAccount
    wx.request({
      url: 'http://localhost:8888/dealing/getDealingById',
      method: 'GET',
      data: {
        id: app.globalData.dealingId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          item: res.data.data
        })
        that.getAccountImg(res.data.data.username)
        that.setData({

          "item.photo": util.strToArr(res.data.data.photo)
        })
        wx.request({
          url: 'http://localhost:8888/collecting/doICollectIt',
          method: 'GET',
          data: {
            username: username,
            dealing: res.data.data.id
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            that.setData({
              isCollected: res.data.data
            })
          }
        })
        wx.request({
          url: 'http://localhost:8888/cart/isInCart',
          method: 'GET',
          data: {
            user: username,
            dealing: res.data.data.id
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            that.setData({
              isInCart: res.data.data
            })
          }
        })
      }
    })
    //console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.hideTabBar({
    //   animation: true,
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getAccountImg: function (id) {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          userimg: res.data.data.headimg,
          nickname: res.data.data.username
        })
      }
    })
  },

  doCollect: function (e) {
    var that = this
    var username = app.globalData.loginAccount
    var id = e.currentTarget.dataset.id
    // console.log(id);
    wx.request({
      url: 'http://localhost:8888/collecting/insertCollecting',
      method: 'POST',
      data: {
        username: username,
        dealing: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.showToast({
          title: '收藏成功',
        })
        that.setData({
          isCollected: 1
        })
      }
    })
  },
  dontCollect: function (e) {
    var that = this
    var username = app.globalData.loginAccount
    var id = e.currentTarget.dataset.id
    // console.log(id);
    wx.request({
      url: 'http://localhost:8888/collecting/deleteCollecting',
      method: 'POST',
      data: {
        username: username,
        dealing: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.showToast({
          title: '取消收藏',
        })
        that.setData({
          isCollected: 0
        })
      }
    })
  },
  enterShop(e) {
    app.globalData.shopId = e.currentTarget.dataset.shop
    //console.log(app.globalData.shopId);
    wx.navigateTo({
      url: '../secondhand-shop/shop',
    })
  },
  showLoginTip() {
    wx.showToast({
      title: '请先登录',
      icon: 'error'
    })
  },
  contact() {
    var user = this.data.item.username
    wx.request({
      url: 'http://localhost:8888/chatting/getChatting',
      method: 'GET',
      data: {
        user1: app.globalData.loginAccount,
        user2: user
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if(res.data.data == null) {
          wx.request({
            url: 'http://localhost:8888/chatting/insertChatting',
            method: 'POST',
            data: {
              user1: app.globalData.loginAccount,
              user2: user
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              app.globalData.chatWho = user
              wx.navigateTo({
                url: '../chat-detail/detail',
              })
            }
          })
        }
        else {
          if(res.data.data.user1 == app.globalData.loginAccount) {
            app.globalData.chatWho = res.data.data.user2
          }
          if(res.data.data.user2 == app.globalData.loginAccount) {
            app.globalData.chatWho = res.data.data.user1
          }
          wx.navigateTo({
            url: '../chat-detail/detail',
          })
        }
      }
    })
  }
})